// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// RESTRICTED USE SOURCE CODE
// http://www.magic-software.com/License/restricted.pdf

#include "MgcController.h"

MgcImplementRTTI(MgcController,MgcObject);
MgcImplementStream(MgcController);

//---------------------------------------------------------------------------
MgcController::MgcController ()
{
    m_eRepeat = RT_CLAMP;
    m_fMinTime = 0.0;
    m_fMaxTime = 0.0;
    m_fPhase = 0.0;
    m_fFrequency = 1.0;
    m_pkObject = 0;
    m_pkNext = 0;
    m_bActive = true;
}
//---------------------------------------------------------------------------
void MgcController::SetObject (MgcObject* pkObject)
{
    m_pkObject = pkObject;
}
//---------------------------------------------------------------------------
MgcReal MgcController::GetControlTime (MgcReal fAppTime)
{
    MgcReal fCtrlTime = m_fFrequency*fAppTime + m_fPhase;

    if ( m_eRepeat == RT_CLAMP )
    {
        if ( fCtrlTime < m_fMinTime )
            return m_fMinTime;
        if ( fCtrlTime > m_fMaxTime )
            return m_fMaxTime;
        return fCtrlTime;
    }

    MgcReal fRange = m_fMaxTime - m_fMinTime;
    if ( fRange > 0.0 )
    {
        MgcReal fMultiples = (fCtrlTime - m_fMinTime)/fRange;
        MgcReal fIntTime = MgcMath::Floor(fMultiples);
        MgcReal fFrcTime = fMultiples - fIntTime;
        if ( m_eRepeat == RT_WRAP )
            return m_fMinTime + fFrcTime*fRange;

        // m_eRepeat == RT_CYCLE
        if ( int(fIntTime) & 1 )
        {
            // backward time
            return m_fMaxTime - fFrcTime*fRange;
        }
        else
        {
            // forward time
            return m_fMinTime + fFrcTime*fRange;
        }
    }
    else
    {
        return m_fMinTime;
    }
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// streaming
//---------------------------------------------------------------------------
MgcObject* MgcController::Factory (MgcStream& rkStream)
{
    // MgcController is abstract, Factory never called
    return 0;
}
//---------------------------------------------------------------------------
void MgcController::Load (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcObject::Load(rkStream,pkLink);

    // native data
    MgcStreamRead(rkStream,m_eRepeat);
    MgcStreamRead(rkStream,m_fMinTime);
    MgcStreamRead(rkStream,m_fMaxTime);
    MgcStreamRead(rkStream,m_fPhase);
    MgcStreamRead(rkStream,m_fFrequency);

    // link data
    MgcObject* pkObject;
    MgcStreamRead(rkStream,pkObject);
    pkLink->Add(pkObject);

    MgcController* pkNext;
    MgcStreamRead(rkStream,pkNext);
    pkLink->Add(pkNext);
}
//---------------------------------------------------------------------------
void MgcController::Link (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcObject::Link(rkStream,pkLink);

    MgcObject* pkLinkID = pkLink->GetLinkID();
    m_pkObject = rkStream.GetFromMap(pkLinkID);

    pkLinkID = pkLink->GetLinkID();
    m_pkNext = (MgcController*)rkStream.GetFromMap(pkLinkID);
}
//---------------------------------------------------------------------------
bool MgcController::Register (MgcStream& rkStream)
{
    if ( !MgcObject::Register(rkStream) )
        return false;

    if ( m_pkObject )
        m_pkObject->Register(rkStream);

    if ( m_pkNext )
        m_pkNext->Register(rkStream);

    return true;
}
//---------------------------------------------------------------------------
void MgcController::Save (MgcStream& rkStream)
{
    MgcObject::Save(rkStream);

    // native data
    MgcStreamWrite(rkStream,m_eRepeat);
    MgcStreamWrite(rkStream,m_fMinTime);
    MgcStreamWrite(rkStream,m_fMaxTime);
    MgcStreamWrite(rkStream,m_fPhase);
    MgcStreamWrite(rkStream,m_fFrequency);

    // link data
    MgcStreamWrite(rkStream,m_pkObject);
    MgcStreamWrite(rkStream,m_pkNext);
}
//---------------------------------------------------------------------------
